package com.example.bijidemo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.bijidemo.common.R;
import com.example.bijidemo.common.SysLog;
import com.example.bijidemo.dto.LogDto;
import com.example.bijidemo.entity.SysLogEntity;
import com.example.bijidemo.service.SysLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("/SysLog")
public class SysLogController {
    @Autowired
    SysLogService sysLogService;
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String username){
        Page<SysLogEntity> pageInfo=new Page<>();
        //根据创建时间排序
        LambdaQueryWrapper<SysLogEntity> a=new LambdaQueryWrapper<>();
        a.orderByDesc(SysLogEntity::getCreateDate);
        sysLogService.page(pageInfo,a);

        List<SysLogEntity> records = pageInfo.getRecords();
        if (username!=null){
            List<SysLogEntity> list= records.stream().filter(s->s.getUsername().contains(username)).collect(Collectors.toList());
            pageInfo.setRecords(list);
            return R.success(pageInfo);

        }
        return R.success(pageInfo);


    }
    @DeleteMapping("/{id}")
    public R<String> delete(@PathVariable Long id){
        sysLogService.removeById(id);
       return  R.success("删除成功");

    }
}
